import fix_path
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from mako.template import Template
from mako.lookup import TemplateLookup
import os

def render_template(path, context={}):
    mako_lookup = TemplateLookup(directories=["%s/templates" % os.path.dirname(__file__)])
    template_path = "%s.mak" % (path)
    template = mako_lookup.get_template(template_path)
    return template.render(**context)
    

def render_path(clazz):
    clazz.response.headers['Content-Type'] = 'text/html'

    path = clazz.request.path
    if path == "/":
        path = "index.html"
    path = path[0:path.find(".")]
    return render_template(path)


class FormHandler(webapp.RequestHandler):
    schema = None
    
    def get(self):
        self.response.out.write(render_path(self))

class PathToTemplate(webapp.RequestHandler):
    def get(self):
        self.response.out.write(render_path(self))

application = webapp.WSGIApplication(
    [
        ('/.*', PathToTemplate),
        ],
    debug=True)
def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
